<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 6] 6.7 Iteration Statements</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:14:31 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch06_06.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 6<br>Statements and Control Structures</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch06_08.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-6-SECT-7">6.7 Iteration Statements</A></h2>

<P CLASS=para>
<A NAME="CH06.ITER1"></A><A NAME="CH06.ITER2"></A><A NAME="CH06.ITER3"></A>Iteration statements
are used to specify the logic of a loop. Java has three varieties
of iteration statement:
<tt CLASS=literal>while</tt>, <tt CLASS=literal>do</tt>, and <tt CLASS=literal>for</tt>.


<p>
<img align=middle src="./figs/jlr0611.gif" alt="[Graphic: Figure from the text]" width=423 height=28 border=0>

<P CLASS=para>
<b>References</b>
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.2">The do Statement</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.3">The for Statement</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.1">The while Statement</A>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-6-SECT-7.1">The while Statement</A></h3>

<P CLASS=para>
A <tt CLASS=literal>while</tt> statement evaluates
a Boolean expression. If the expression is <tt CLASS=literal>true</tt>,
a given statement is repeatedly executed for as long as the expression
continues to evaluate to <tt CLASS=literal>true</tt>.


<p>
<img align=middle src="./figs/jlr0612.gif" alt="[Graphic: Figure from the text]" width=424 height=27 border=0>

<P CLASS=para>
In Java, the expression in parentheses must produce a <tt CLASS=literal>boolean</tt> value. This is different from C/C++, which allows any
type of expression.

<P CLASS=para>
If the expression in parentheses
evaluates to <tt CLASS=literal>true</tt>, the statement contained in
the <tt CLASS=literal>while</tt> statement is executed and the expression
in parentheses is evaluated again. This process continues until
the expression evaluates to <tt CLASS=literal>false</tt>.

<P CLASS=para>
If the expression in parentheses evaluates to <tt CLASS=literal>false</tt>,
the statement following the <tt CLASS=literal>while</tt> statement is
the next statement to be executed. The expression in parentheses
is evaluated before the contained statement is executed, so it is
possible for the contained statement not to be executed even once.

<P CLASS=para>
Here is an example of a <tt CLASS=literal>while</tt> statement:

<DIV CLASS=screen>
<P>
<PRE>
while ( (c = in.read()) &gt;= 0) {
    out.write(c);
}
</PRE>
</DIV>

<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.2">Boolean Type</A>;
<I CLASS=emphasis>Expression</I> 4;
<I CLASS=emphasis>Statement</I> 6

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-6-SECT-7.2">The do Statement</A></h3>

<P CLASS=para>
A <tt CLASS=literal>do</tt> statement executes a given statement
and then evaluates a Boolean expression. If the expression evaluates
to <tt CLASS=literal>true</tt>, the statement is executed repeatedly
as long as the expression continues to evaluate to <tt CLASS=literal>true</tt>:


<p>
<img align=middle src="./figs/jlr0613.gif" alt="[Graphic: Figure from the text]" width=424 height=105 border=0>

<P CLASS=para>
In Java, the expression in parentheses must produce a
<tt CLASS=literal>boolean</tt>
value. This is unlike C/C++, which allows any type of expression.

<P CLASS=para>
The statement contained in the <tt CLASS=literal>do</tt> statement
is executed and then the expression in parentheses is evaluated.
If the expression evaluates to <tt CLASS=literal>true</tt>, the process
is repeated.

<P CLASS=para>
If the expression evaluates to false, the
statement following the <tt CLASS=literal>do</tt> statement is the next
statement to be executed. Because the expression is evaluated after
the contained statement is executed, the statement is always executed
at least once.

<P CLASS=para>
Here's an example of a <tt CLASS=literal>do</tt> statement:

<DIV CLASS=screen>
<P>
<PRE>
do {
    c = in.read();
    out.write(c);
} while (c != ';');
</PRE>
</DIV>

<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.2">Boolean Type</A>;
<I CLASS=emphasis>Expression</I> 4;
<I CLASS=emphasis>Statement</I> 6

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-6-SECT-7.3">The for Statement</A></h3>

<P CLASS=para>
<A NAME="CH06.FOR"></A>A <tt CLASS=literal>for</tt>
statement is a more structured form of a <tt CLASS=literal>while</tt>
statement. A <tt CLASS=literal>for</tt> statement performs an initialization
step and then evaluates a Boolean expression. If the expression
evaluates to <tt CLASS=literal>true</tt>, a given statement is executed
and an increment expression is evaluated repeatedly as long as the
expression continues to evaluate to <tt CLASS=literal>true</tt>:


<p>
<img align=middle src="./figs/jlr0614.gif" alt="[Graphic: Figure from the text]" width=424 height=77 border=0>


<p>
<img align=middle src="./figs/jlr0615.gif" alt="[Graphic: Figure from the text]" width=450 height=43 border=0>


<p>
<img align=middle src="./figs/jlr0616.gif" alt="[Graphic: Figure from the text]" width=424 height=46 border=0>

<P CLASS=para>
Here is an example of a <tt CLASS=literal>for</tt> statement:

<DIV CLASS=screen>
<P>
<PRE>
for (i = 0; i &lt; a.length; i++) {
    a[i] = i;
}
</PRE>
</DIV>

<P CLASS=para>
The initialization
part of the <tt CLASS=literal>for</tt> statement is executed first.
If the initialization part contains nothing, no initialization is
performed. The expression that follows must produce a
<tt CLASS=literal>boolean</tt>
value. Before the body of the <tt CLASS=literal>for</tt> statement is
executed, the expression is evaluated. If the expression portion
of the <tt CLASS=literal>for</tt> statement is omitted, the default
expression <tt CLASS=literal>true</tt> is used. If the expression evaluates
to <tt CLASS=literal>true</tt>, the body of the <tt CLASS=literal>for</tt>
statement is executed and then the increment portion of the
<tt CLASS=literal>for</tt>
statement is evaluated. Finally, the expression is evaluated again
to determine if there should be another iteration. This process
continues until the expression evaluates to <tt CLASS=literal>false</tt>,
at which point the statement following the <tt CLASS=literal>for</tt>
statement is the next statement to be executed.
The <tt CLASS=literal>for</tt> statement in the above example can be rewritten
as a <tt CLASS=literal>while</tt> statement as follows:

<DIV CLASS=screen>
<P>
<PRE>
i = 0;
while (i &lt; a.length) {
    a[i] = i;
    i++;
}
</PRE>
</DIV>

<P CLASS=para>
One difference between
comparable <tt CLASS=literal>for</tt> and <tt CLASS=literal>while</tt> loops
is that a <tt CLASS=literal>continue</tt> statement in the body of a
<tt CLASS=literal>for</tt> statement causes the increment portion of
the statement to be evaluated. However, this may not be the case
in a comparable <tt CLASS=literal>while</tt> statement.

<P CLASS=para>
Here's a new
version of our <tt CLASS=literal>for</tt> example:

<DIV CLASS=screen>
<P>
<PRE>
for (i = 0; i &lt; a.length; i++) {
    a[i] = i;
    continue;
}
</PRE>
</DIV>

<P CLASS=para>
The added <tt CLASS=literal>continue</tt> statement at the end
of the <tt CLASS=literal>for</tt> loop
does not change the behavior of the loop. In particular, <tt CLASS=literal>i++</tt>
is still evaluated after each iteration through the body of the
loop. Now let's add a <tt CLASS=literal>continue</tt> statement at the
equivalent place in our <tt CLASS=literal>while</tt> example:

<DIV CLASS=screen>
<P>
<PRE>
i = 0;
while (i &lt; a.length) {
    a[i] = i;
    continue;
    i++;
}
</PRE>
</DIV>

<P CLASS=para>
The <tt CLASS=literal>continue</tt>
statement in this <tt CLASS=literal>while</tt> loop prevents the statement
<tt CLASS=literal>i++</tt> from being executed. The <tt CLASS=literal>continue</tt>
statement would have to be moved after the increment operation to
match the logic of the <tt CLASS=literal>for</tt> statement.

<P CLASS=para>
If the expression portion of a <tt CLASS=literal>for</tt> statement is
omitted, the default expression <tt CLASS=literal>true</tt> is supplied.
Take, for example, the following <tt CLASS=literal>for</tt> statement:

<DIV CLASS=screen>
<P>
<PRE>
for ( FileInputStream in = new FileInputStream(fname);;) {
    c = in.read();
    if (c &lt; 0)
        return;
    System.out.print((char)c);
}
</PRE>
</DIV>

<P CLASS=para>
This example uses a local variable declaration in the initialization
portion of the <tt CLASS=literal>for</tt> statement. Local variable
declarations in a <tt CLASS=literal>for</tt>
statement are subject to the same restrictions as local variable
declarations in a block. In particular, a <tt CLASS=literal>for</tt>
statement cannot declare a local variable with the same name as
a local variable or formal parameter that is defined in an enclosing
block.

<P CLASS=para>
The above <tt CLASS=literal>for</tt> statement is
equivalent to the following <tt CLASS=literal>while</tt> statement:

<DIV CLASS=screen>
<P>
<PRE>
{
    FileInputStream in = new FileInputStream(fname);
    while (true) {
        c = in.read();
        if (c &lt; 0)
            return;
        System.out.print((char)c);
    }
}
</PRE>
</DIV>

<P CLASS=para>
The enclosing block in the above example is provided to limit
the scope of the local variable <tt CLASS=literal>in</tt> to just the
<tt CLASS=literal>while</tt> statement.

<P CLASS=para>
The initialization
portion of a <tt CLASS=literal>for</tt> statement can also be empty.
The following statement is a legal way of specifying an infinite loop:

<DIV CLASS=screen>
<P>
<PRE>
for (;;) {...}
</PRE>
</DIV>

<P CLASS=para>
This is equivalent
to the following <tt CLASS=literal>while</tt> statement:

<DIV CLASS=screen>
<P>
<PRE>
while (true) {...}
</PRE>
</DIV>

<P CLASS=para>
Unlike C/C++,
there is no comma operator in Java. However, commas are explicitly
allowed in the initialization portion of a <tt CLASS=literal>for</tt> statement.
For example, a <tt CLASS=literal>for</tt> initialization can consist
of multiple expressions separated by commas:

<DIV CLASS=screen>
<P>
<PRE>
i=2, j=5, k=44
</PRE>
</DIV>

<P CLASS=para>
When the initialization
portion of a <tt CLASS=literal>for</tt> statement contains local variable
declarations, commas are also allowed because the syntax for declarations
allows multiple variables, separated by commas, to be declared in one declaration. For
example:

<DIV CLASS=screen>
<P>
<PRE>
int i=2, j=5, k=44
</PRE>
</DIV>

<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.2">Boolean Type</A>;
<I CLASS=emphasis>Expression</I> 4;
<I CLASS=emphasis>Statement</I> 6;
<A HREF="ch06_01.htm#JLR2-CH-6-SECT-1.1">Local Variables</A>;
<I CLASS=emphasis>TopLevelExpression</I> 6.4;
<A HREF="ch06_09.htm#JLR2-CH-6-SECT-9">The continue Statement</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.1">The while Statement</A>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch06_06.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch06_08.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>The switch Statement</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>The break Statement</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
